/**
 * 
 */
package travibot.meta;

import java.util.HashMap;
import java.util.Map;

import travibot.model.Cost;
import travibot.utility.TimeSpan;

/**
 * @author Petr Macek
 * 
 */
public class StructureType {

    private int id;
    private String implementingClassName;
    private String name;
    private int maxLevel;
    private int maxLevelMainVillage;
    private int gameId;
    private Map<Integer, Cost> costsPerLevel = new HashMap<Integer, Cost>();
    private Map<Integer, TimeSpan> constructionDurationsPerLevel = new HashMap<Integer, TimeSpan>();
    private MaterialType material;

    
    
    public StructureType() {

        super();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {

        boolean ret = false;
        if (obj instanceof StructureType) {
            if (id == ((StructureType)obj).id) {
                ret = true;
            }
        }
        return ret;
    }

    public int getId() {

        return id;
    }

    public String getImplementingClassName() {

        return implementingClassName;
    }

    public String getName() {

        return name;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {

        return String.valueOf(id).hashCode();
    }

    public void setId(int id) {

        this.id = id;
    }

    public void setImplementingClassName(String implementingClassName) {

        this.implementingClassName = implementingClassName;
    }

    public void setName(String name) {

        this.name = name;
    }

    /**
     * Returns maximum level of current structure depending on the villiage type
     * the structure is in. Structures in main village can have higher levels.
     * 
     * @param mainVillage
     * @return maximum level
     */
    public int getMaxLevel(boolean mainVillage) {

        int ret;
        if (mainVillage) {
            ret = maxLevelMainVillage;
        }
        else {
            ret = maxLevel;
        }
        return ret;
    }

    public void setMaxLevel(int maxLevel) {

        this.maxLevel = maxLevel;
    }

    public void setMaxLevelMainVillage(int maxLevelMainVillage) {

        this.maxLevelMainVillage = maxLevelMainVillage;
    }
    
    /**
     * Returns maximum available level either for main or any other village.
     * If -1 is returned, the level is unlimited at least for one of the cases.
     * @return
     */
    public int getMaxAvailableLevel(){
        int ret;
        if(getMaxLevel(true) < 0 || getMaxLevel(false) < 0){
            ret = -1;
        } else {
            ret = Math.max(getMaxLevel(true), getMaxLevel(false));
        }        
        return ret;
    }

    public MaterialType getMaterial() {

        return material;
    }

    public void setMaterial(MaterialType material) {

        this.material = material;
    }
    

    /**
     * Id in Travian game
     * @return
     */
    public int getGameId() {

        return gameId;
    }

    /**Id in Travian game
     * @param gameId
     */
    public void setGameId(int gameId) {

        this.gameId = gameId;
    }

    /**
     * Method adds construction cost per specified level.
     * @param level
     * @param cost
     */
    public void addCost(int level, Cost cost){
        if(!costsPerLevel.containsKey(level)){
            costsPerLevel.put(level, cost);
        }
    }
    
    public Cost getCost(int level){
        return costsPerLevel.get(level);
    }
    
    
    
}
